package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.CategoryMapper;
import com.dmpp.mes.dao.MaterialClassificationMapper;
import com.dmpp.mes.parameterInfo.Category;
import com.dmpp.mes.parameterInfo.MaterialClassification;
import com.dmpp.mes.service.CategoryService;
import com.dmpp.mes.service.MaterialClassificationService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "CategoryService")

public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper mapper;

    @Override
    public ResponseData findAll(Category category) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(category));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(Category category) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(category));
        return responseData;
    }

    @Override
    public ResponseData insert(Category category) {
        ResponseData responseData = new ResponseData();
       Category category1= mapper.findByCode(category);
       if (category1==null){
           mapper.insert(category);
       }else {
           responseData.setCodeEnum(RespEnum.ERROR2001);
       }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(Category category) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(category);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(Category category) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(category);
        return responseData;
    }

    @Override
    public ResponseData delete(Category category) {
        ResponseData responseData = new ResponseData();
        Integer number=mapper.count(category);
        Integer semiProductCount=mapper.semiProductCount(category);
        Integer productCount=mapper.productCount(category);
        if (category.getMakingsState().equals("原材料")){
            if (number==0){
                mapper.delete(category);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1016);
            }
        }
        if (category.getMakingsState().equals("半成品")){
            if (semiProductCount==0){
                mapper.delete(category);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1018);
            }
        }

        if (category.getMakingsState().equals("成品笔")){
            if (productCount==0){
                mapper.delete(category);
            }else {
                responseData.setCodeEnum(RespEnum.ERROR1017);
            }
        }

        return responseData;
    }


}
